package com.fengye.webflux.mongo.config;

import com.fengye.webflux.repository.ArticleRepository;
import com.mongodb.reactivestreams.client.MongoClient;
import com.mongodb.reactivestreams.client.MongoClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractReactiveMongoConfiguration;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories;

/**
 *
 * @author sky
 * @date 2021年06月01日 1:38 下午
 */
@Configuration
@EnableReactiveMongoRepositories(basePackageClasses = ArticleRepository.class)
public class MongoConfig extends AbstractReactiveMongoConfiguration {

    @Bean
    @Override
    public MongoClient reactiveMongoClient() {
        return MongoClients.create();
    }


    @Override
    protected String getDatabaseName() {
        return "article_test";
    }

    @Bean
    public ReactiveMongoTemplate reactiveMongoTemplate() {
        return new ReactiveMongoTemplate(reactiveMongoClient(), getDatabaseName());
    }

    @Bean
    public MongoOperations operations() {
        return new MongoTemplate(com.mongodb.client.MongoClients.create(), getDatabaseName());
    }
}
